package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.beans.UsuarioBean;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import business.Usuario;

import controler.forms.PasswordSetForm;

public class PasswordSetAction extends MappingDispatchAction {

	private static final String DEFAULT = "default";
	private static UsuarioBean user_log_t = null;
	
	private ActionForward validateSession(ActionMapping mapping, HttpServletRequest request)throws Exception {
		ActionForward action_retVal = null;
		HttpSession session = request.getSession();
		user_log_t =(UsuarioBean)session.getAttribute("ses_usuario");
    	int int_validaSession = Usuario.validarSession(user_log_t);
    	if(int_validaSession==Usuario.SESSIONNOVALIDA){
    		action_retVal = mapping.findForward("session_invalid");
    	}
    	return action_retVal;
	}
	
		public ActionForward setPassword(
				ActionMapping mapping, 
				ActionForm form, 
				HttpServletRequest request, 
				HttpServletResponse response) throws Exception {
			
			ActionForward action_retVal = validateSession(mapping,request);
	    	if(action_retVal!=null)return action_retVal;
	    	
			PasswordSetForm forma = (PasswordSetForm)form;
			
			String str_login       = request.getParameter("login");
			String str_password    = forma.getStr_password();
			String str_newPassword = forma.getStr_newPassword();
			
			boolean bool_update = model.User.passwordUpdate(str_login, str_password, str_newPassword);
			MessageResources msg = this.getResources(request);
			
			if(bool_update){
				String str_msg = msg.getMessage("user.password.success");
				request.setAttribute("req_msg", str_msg);
			}
			if(!	bool_update){
				String str_msg = msg.getMessage("user.password.fail");
				request.setAttribute("req_msg", str_msg);
			}
			return mapping.findForward(DEFAULT);
		}
}
